<template>
    <div class="bg-fff h-100p bdr-10 pd-10">
        <v-plain-table :native="{stripe:true}" :data="channelProviders" @change="change">
            <template slot="expand" slot-scope="{scope}">
                <el-button type="text" @click="$router.push(`/platform/salesmen/detail/${scope.row.id}`)">
                    详情
                </el-button>
                <el-button type="text" @click="showEdit(scope.row)" v-if="userInfo.roleType === 1">编辑</el-button>
                <el-button type="text" @click="showAudit(scope.row)" v-if="scope.row.userStatus === 0">审核</el-button>
            </template>
        </v-plain-table>
        <v-audit-channel-provider ref="audit-channel-provider" @initList="getChannelProviders"/>
        <v-edit-channel-provider ref="edit-channel-provider" @initList="getChannelProviders"/>
    </div>
</template>

<script lang="ts">
  import { Vue, Component } from 'vue-property-decorator'
  import vPlainTable from '@/components/plain-table'
  import vAuditChannelProvider from '../audit'
  import vEditChannelProvider from '../edit'

  @Component({
    components: {
      vPlainTable,
      vAuditChannelProvider,
      vEditChannelProvider
    }
  })
  export default class Platform extends Vue {
    channelProviders: TableData = {
      header: [
        {
          label: 'id',
          key: 'id'
        },
        {
          label: '渠道商名称',
          key: 'channelName'
        },
        {
          label: '会员数量',
          key: 'memberCount'
        },
        {
          label: '业务员数量',
          key: 'salesmanCount'
        },
        {
          label: '利润配比',
          key: 'profitRatio'
        },
        {
          label: '联系方式',
          key: 'phone'
        },
        {
          label: '创建日期',
          key: 'createTime',
          formatter: row => this.$utils.toTimeFormat(row.createTime)
        },
        {
          label: '最后登录',
          key: 'date',
          formatter: row => this.$utils.toTimeFormat(row.lastLoginTime)
        }
      ],
      body: [],
      footer: {
        total: 0,
        currentPage: 1
      }
    }
    url = ''
    userId = ''
    userInfo = {}
    async getChannelProviders () {
      let res: AjaxResponse = await  this.$axios.post(this.url, {
        pageSize: 10,
        currentPage: this.channelProviders.footer.currentPage,
        userId: this.userId
      })
      this.channelProviders.body = res.data.items
      this.channelProviders.footer.total = res.data.totalNum
      this.channelProviders.footer.currentPage = res.data.currentPage
    }

    mounted () {
      let userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'))
      this.userInfo = userInfo;
      // 没有id  大掌柜
      if (this.$route.params.id) {
        this.url = 'b/boms/channelprovider/channelcommissioner/channelproviders'
        this.userId = this.$route.params.id
      } else {
        if (userInfo.roleType === 3 || userInfo.roleType === 6) {
          this.url = 'b/boms/channelprovider/channelcommissioner/channelproviders'
        } else {
          this.url = 'b/boms/channelprovider/channelproviders'
        }
      }
      this.getChannelProviders()      
    }

    change (page) {
      this.channelProviders.footer.currentPage = page
      this.getChannelProviders()
    }

    async showAudit (row) {
      let res: AjaxResponse = await this.$axios.get(`g/gateway/channelprovider/${row.id}`)
      this.$refs['audit-channel-provider'].openDialog(res.data)
    }
    async showEdit (row) {
      let res: AjaxResponse = await this.$axios.get(`g/gateway/channelprovider/${row.id}`)
      this.$refs['edit-channel-provider'].openDialog(res.data)
    }

  }
</script>

<style lang="less" scoped>
    .bdr-bottom-10 {
        overflow-y: scroll;
    }
</style>
